const p1 = new Promise((reject) => {
    setTimeout(() => reject(1), 1000);
});

const p2 = new Promise((reject) => {
    setTimeout(() => reject(2), 0);
});

const p3 = new Promise((reject) => {
    setTimeout(() => reject(3), 500);
});

function pAll(promiseList) {
    let count = 0;
    const values = [];
    return new Promise((resolve, reject) => {
        promiseList.forEach((item, index) => {
            Promise.resolve(item).then(
                (rsp) => {
                    values[index] = rsp;
                    count++;
                    if (count == promiseList.length) {
                        resolve(values);
                    }
                },
                (err) => {
                    reject(err);
                }
            );
        });
    });
}

pAll([p1, p2, p3]).then((res) => {
    console.log("Promise All's values:", res);
});

function pRace(promiseList) {
    return new Promise((resolve, reject) => {
        promiseList.forEach((item) => {
            item.then(
                (res) => {
                    resolve(res);
                },
                (err) => {
                    reject(err);
                }
            );
        });
    });
}

pRace([p1, p2, p3]).then((res) => {
    console.log("Promise Race's values:", res);
});

Promise.race([p1, p2, p3]).then((res) => {
    console.log("Promise Race's values:", res);
});
